package practice.P01M03;

import org.junit.jupiter.api.Test;

import java.util.*;

public class PlayCollection {
    @Test
    public void listTest() {
        Collection<Object> col = new ArrayList<>(); // 接口类引用指向其实现类，形成多态
        col.add("one");
        col.add("three");
        System.out.println(col); // [one, three]
        Collection<String> col2 = new ArrayList<>();
        col2.add("two");
        col.addAll(col2);
        System.out.println(col); // [one, three, two]
        boolean contains = col.contains("one");
        System.out.println(contains); // true
        Person p = new Person("alex", 20);
        col.add(p);
        Person p1 = new Person("alex", 20);
        contains = col.contains(p1);
        System.out.println(contains); // false, compare address by default, if you didn't override equals method
        contains = col.containsAll(col2);
        System.out.println(contains); // true
        System.out.println(col); // [one, three, two, Person{name='alex', age=20}]
        contains = col.retainAll(col);
        System.out.println(contains); // false, 当col元素发生变化时，返回true
        contains = col.retainAll(col2);
        System.out.println(contains); // true, 保留交集
        System.out.println(col); // [two]
        col.add("one");
        col.add("two");
        col.add("three");
        col.add(p);
        col.remove("three");
        System.out.println(col); // [two, one, two, Person{name='alex', age=20}]
        col2.add("four");
        col.removeAll(col2); // 减法
        System.out.println(col); // [one, Person{name='alex', age=20}]
        final Object[] objects = col.toArray();
        System.out.println(Arrays.toString(objects)); // [one, Person{name='alex', age=20}]
        final List<Object> objects1 = Arrays.asList(objects);
        System.out.println(objects1); // [one, Person{name='alex', age=20}]
        for (Object next : col) {
            System.out.println(next);
        }
    }
}
